package com.bgs.service.impl;

import com.bgs.mapper.FinanceMapper;
import com.bgs.pojo.FinWrite;
import com.bgs.pojo.Finance;
import com.bgs.service.FinanceService;
import com.bgs.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: xkgc
 * @description:
 * @author: Mr.Zhao
 * @create: 2022-05-17 09:49
 */
@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private FinanceMapper financeMapper;

    @Override
    public List<Finance> findAllFinance(String number, Integer orderType) {
        return financeMapper.findAllFinance(number,orderType);
    }

    @Override
    public Finance findAllMoney(Integer storeId) {
        return financeMapper.findAllMoney(storeId);
    }

    @Override
    public int addMoney(FinWrite finWrite) {
        LocalDateTime localDateTime = LocalDateTime.now();
               //门店钱包
                   if (finWrite.getMoney()==980){
                       Double wallet = finWrite.getWallet();
                       Double money = finWrite.getMoney();
                       Double mm =money*10;

                       finWrite.setWallet(wallet+mm+200);
                   }else {
                       Double wallet = finWrite.getWallet();
                       Double money = finWrite.getMoney();
                       Double mm =money*10;
                       finWrite.setWallet(wallet+mm);
                   }


               //收入
        if (finWrite.getMoney()==980){
            finWrite.setIncome((finWrite.getMoney()+20)*10);
        }else {
            finWrite.setIncome(finWrite.getMoney()*10);
        }

                //订单类型
                finWrite.setOrderType(3);
                //支付状态
                finWrite.setFinanceState(1);
                //订单编号
        String orderCode = OrderUtils.getOrderCode(finWrite.getWriteoffId());
        finWrite.setNumber(orderCode);
                //创建时间
              finWrite.setCreationTime(localDateTime);
              finWrite.setUpdateTime(localDateTime);
              finWrite.setIsDel("n");


              //订单编号
              finWrite.setOrdernumber(orderCode);
              //订单类型
              finWrite.setType(1);
              //支付状态
                finWrite.setPaymentState(1);
            //支付流水
                finWrite.setSerial(OrderUtils.getDateTime());
            //支付时间
                finWrite.setPaymentTime(localDateTime);

        financeMapper.addWrite(finWrite);
        financeMapper.addFinance(finWrite);


        return financeMapper.addMoney(finWrite);
    }

    @Override
    public int zhiFuMoney(FinWrite finWrite) {
        LocalDateTime localDateTime = LocalDateTime.now();

        //收入
        if (finWrite.getMoney()==980){
            finWrite.setIncome((finWrite.getMoney()+20)*10);
        }else {
            finWrite.setIncome(finWrite.getMoney()*10);
        }
        //订单类型
        finWrite.setOrderType(3);
        //支付状态
        finWrite.setFinanceState(3);
        //订单编号
        String orderCode = OrderUtils.getOrderCode(finWrite.getStoreId());
        finWrite.setNumber(orderCode);
        //创建时间
        finWrite.setCreationTime(localDateTime);
        finWrite.setUpdateTime(localDateTime);
        finWrite.setIsDel("n");


        //订单编号
        finWrite.setOrdernumber(orderCode);
        //订单类型
        finWrite.setType(1);
        //支付状态
        finWrite.setPaymentState(0);
        //支付流水
        finWrite.setSerial(OrderUtils.getDateTime());
        //支付时间
        finWrite.setPaymentTime(localDateTime);

        financeMapper.addWrite(finWrite);
        financeMapper.addFinance(finWrite);


        return financeMapper.addMoney(finWrite);
    }

    @Override
    public int caiWuUp(FinWrite finWrite) {

        LocalDateTime localDateTime = LocalDateTime.now();
        finWrite.setWallet(finWrite.getWallet()+finWrite.getIncome());
        finWrite.setFinanceState(1);
        finWrite.setPaymentState(1);
        finWrite.setPaymentTime(localDateTime);

        financeMapper.addWritee(finWrite);
        financeMapper.addFinancee(finWrite);


        return financeMapper.addMoneyy(finWrite);
    }
}
